﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FgoDataCrawler.Utility
{
    public abstract class NumberConvert
    {
        public const int GenderNone = 0;     // 000 无
        public const int GenderFemale = 1;   // 001 女性
        public const int GenderMale = 2;     // 010 男性
        public const int GenderBoth = 3;     // 011 皆有
        public const int GenderUnknown = 4;  // 100 未知

        public static double PercentToDouble(string p)
        {
            if (string.IsNullOrEmpty(p)) return 0;
            if (p.Contains("&"))
            {
                var idx = p.IndexOf("&");
                p = p.Substring(0, idx).Trim();
            }
            bool isPercent = false;
            if (p.Contains("%"))
            {
                p = p.Replace("%", "").Trim();
                isPercent = true;
            }
            try
            {
                var f = Double.Parse(p);
                if (isPercent) f = f / 100.0;
                return f;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return 0;
        }

        public static int PercentToInt(string p)
        {
            if (string.IsNullOrEmpty(p)) return 0;
            if (p.Contains("&"))
            {
                var idx = p.IndexOf("&");
                p = p.Substring(0, idx).Trim();
            }
            try
            {
                var d = Int32.Parse(p);
                return d;
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return 0;
        }

        public static int ToGenderId(string desc)
        {
            if (desc == "男性") return GenderMale;
            if (desc.StartsWith("女性")) return GenderFemale;
            if (desc == "？？？") return GenderUnknown;
            if (desc == "？") return GenderUnknown;
            if (desc == "男性・女性") return GenderBoth;
            if (desc == "依据个体而不同") return GenderUnknown;
            if (desc == "－") return GenderNone;
            if (desc == "无") return GenderNone;

            Console.WriteLine("无法解析的性别：" + desc);

            return GenderUnknown;
        }
    }
}
